package com.ruoyi.api.service.impl;

import com.ruoyi.api.domain.UserEntity;
import com.ruoyi.api.repository.UserRepository;
import com.ruoyi.api.service.UserService;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.model.RegistParam;
import com.ruoyi.common.exception.ServiceException;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
@Log4j2
@Service
public class UserSeriviceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public UserEntity selectUserByEmail(String email) {
        return userRepository.findByEmail(email);
    }

    @Override
    public void regist(RegistParam registParam) {
        UserEntity userEntity = userRepository.findByEmail(registParam.getEmail());
        if(null!=userEntity){
            throw new ServiceException("This email address has been registered");
        }
        UserEntity entity = new UserEntity();
        entity.setEmail(registParam.getEmail());
        entity.setNickName(registParam.getNickName());
//        entity.setPassWord(Argon2Utils.hashPassword(registParam.getPassword()));
        entity.setPassWord(SecurityUtils.encryptPassword(registParam.getPassword()));
        userRepository.save(entity);
    }

    @Override
    public void validatePassword(String passWord, String code) {
//        if(!Argon2Utils.verifyPassword(passWord,code)){
//            throw new ServiceException("Password fail");
//        }
        if(!SecurityUtils.matchesPassword(passWord,code)){
            throw new ServiceException("Password fail");
        }
    }

    @Override
    public Page<UserEntity> selectUserList(UserEntity user, int page, int size) {
        Query query = new Query();
//        String delFlag = "0"; // 代表未删除用户

        // 添加过滤条件
//        query.addCriteria(Criteria.where("del_flag").is(delFlag));

        if (StringUtils.isNotNull(user.getEmail())) {
            query.addCriteria(Criteria.where("email").regex(user.getEmail(), "i"));
        }


        // 设置分页
        Pageable pageable = PageRequest.of(page, size);
        query.with(pageable);

        // 执行查询
        List<UserEntity> users = mongoTemplate.find(query, UserEntity.class);

        // 获取总记录数
        long total = mongoTemplate.count(query, UserEntity.class);

        return new PageImpl<UserEntity>(users, pageable, total);
    }

    @Override
    public void updateAvatar(UserEntity user) {
        Query query = new Query();
        query.addCriteria(Criteria.where("email").is(user.getEmail()));

        Update update = new Update();
        update.set("avatar", user.getAvatar());

        mongoTemplate.upsert(query,update,UserEntity.class);
    }

}
